package com.dream.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.dream.classcontrol.MainActivity;
import com.dream.classcontrol.R;
import com.dream.config.DevUIIDConfig;
import com.dream.sdkmanager.SDKManager;


public class DengguanFragment extends android.app.Fragment implements View.OnClickListener {

    private ImageView lampOne;
    private ImageView lampTwo;
    private ImageView dengguanOneOpen;
    private ImageView dengguanOneClose;
    private ImageView dengguanTwoOpen;
    private ImageView dengguanTwoClose;
    private int oneState=0;
    private int twoState=0;

    private MainActivity mainActivity;
    private SDKManager sdkManager;

    public DengguanFragment() {
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity= (MainActivity) activity;
        sdkManager=mainActivity.getSdkManager();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_dengguan, container, false);
        initView(view);
        setOnClick();
        return view;
    }

    public void initView(View view){
        lampOne = (ImageView) view.findViewById(R.id.lampOne);
        lampTwo = (ImageView) view.findViewById(R.id.lampTwo);
        dengguanOneOpen = (ImageView) view.findViewById(R.id.dengguan_one_open);
        dengguanOneClose = (ImageView)  view.findViewById(R.id.dengguan_one_close);
        dengguanTwoOpen = (ImageView)  view.findViewById(R.id.dengguan_two_open);
        dengguanTwoClose = (ImageView)  view.findViewById(R.id.dengguan_two_close);
    }

    public void setOnClick(){
        dengguanOneOpen.setOnClickListener(this);
        dengguanOneClose.setOnClickListener(this);
        dengguanTwoOpen.setOnClickListener(this);
        dengguanTwoClose.setOnClickListener(this);
    }

    public void controlDengGuan(String devId,Integer state){
        if (sdkManager.getLandingSate()){
            sdkManager.ControlDev(DevUIIDConfig.diandeng,devId,state);
        }else {
            Toast.makeText(getActivity(),"请登录后重试",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dengguan_one_open:
                oneState=1;
                lampOne.setImageResource(R.drawable.icon_dengguan_one_selected);
                controlDengGuan("devId_1",1);
                break;
            case R.id.dengguan_one_close:
                oneState=0;
                lampOne.setImageResource(R.drawable.icon_dengguan_one_unselect);
                controlDengGuan("devId_1",2);
                break;
            case R.id.dengguan_two_open:
                twoState=1;
                lampTwo.setImageResource(R.drawable.icon_dengguan_two_selected);
                controlDengGuan("devId_2",3);
                break;
            case R.id.dengguan_two_close:
                twoState=0;
                lampTwo.setImageResource(R.drawable.icon_dengguan_two_unselect);
                controlDengGuan("devId_2",4);
                break;
        }
        setImageViewBg();
    }

    //true开 false关
    public void setImageViewBg(){
        if (oneState==0){
            dengguanOneOpen.setImageResource(R.drawable.bg_dengguan_open_unselect);
            dengguanOneClose.setImageResource(R.drawable.bg_dengguan_close_selected);
        }else if (oneState==1){
            dengguanOneOpen.setImageResource(R.drawable.bg_dengguan_open_selected);
            dengguanOneClose.setImageResource(R.drawable.bg_dengguan_close_unselect);
        }

        if (twoState==0){
            dengguanTwoOpen.setImageResource(R.drawable.bg_dengguan_open_unselect);
            dengguanTwoClose.setImageResource(R.drawable.bg_dengguan_close_selected);
        }else if (twoState==1){
            dengguanTwoOpen.setImageResource(R.drawable.bg_dengguan_open_selected);
            dengguanTwoClose.setImageResource(R.drawable.bg_dengguan_close_unselect);
        }

    }

}
